<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">

          <a-col :md="6" :sm="24">
            <a-form-item label="配置名称">
              <a-input v-model="queryParam.configName" allow-clear placeholder="请输入配置名称"/>
            </a-form-item>
          </a-col>

          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons" style="padding-left: 16px;" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)" class="btn_search_reset">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}" class="btn_search_reset">重置</a-button>
            </span>
          </a-col>
          <a-col :md="!advanced && 10 || 24" :sm="24">
            <span class="table-page-search-submitButtons" style="float: right;" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.addForm.add()" class="btn_add" icon="plus">新增</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.id"
      id="table-wrapper"

    >
      <span slot="status" slot-scope="text">
        {{ statusFliter(text) }}
      </span>
      <span slot="action" slot-scope="text, record">
        <a @click="$refs.editForm.edit(record)" class="btn_edit">编辑</a>
        <a-divider type="vertical"/>
        <a-popconfirm class="btn_del" placement="topRight" title="确认删除？" @confirm="() => del(record)">
          <a>删除</a>
        </a-popconfirm>
      </span>
      <span slot="configType" slot-scope="text">
        <template v-if="text==='1'">字符型</template>
        <template v-if="text==='2'">布尔型</template>
        <template v-if="text==='3'">整型</template>
        <template v-if="text==='4'">单选</template>
      </span>
      <span slot="configCategory" slot-scope="text">
        <template v-if="text==='1'">基础配置</template>
        <template v-if="text==='2'">高级配置</template>
      </span>
    </s-table>

    <add-form ref="addForm" @ok="handleOk"/>
    <edit-form ref="editForm" @ok="handleOk"/>
  </a-card>
</template>

<script>
  import { STable } from '@/components'
  import { page, del, tree } from '@/api/modular/main/bizgatewayconfigtemplate/bizGatewayConfigTemplateManage'
  import addForm from './addForm'
  import editForm from './editForm'

  export default {
    components: {
      STable,
      addForm,
      editForm
    },

    data () {
      return {
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        columns: [ // 表头
          {
            title: '名称',
            dataIndex: 'configName',
            width: 150,
            ellipsis: true
          },
          {
            title: '编码',
            dataIndex: 'code',
            width: 300
          },
          {
            title: '类型',
            dataIndex: 'configType',
            scopedSlots: { customRender: 'configType' }
          },
          {
            title: '值',
            dataIndex: 'value',
            ellipsis: true
          },
          {
            title: '默认值',
            dataIndex: 'defaultValue',
            ellipsis: true
          },
          {
            title: '最小值',
            dataIndex: 'minValue'
          },
          {
            title: '最大值',
            dataIndex: 'maxValue'
          },
          {
            title: '排序',
            dataIndex: 'orderNum'
          },
          {
            title: '配置分组',
            dataIndex: 'configCategory',
            scopedSlots: { customRender: 'configCategory' }
          },
          {
            title: '状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' }
          },
          {
            title: '备注',
            dataIndex: 'remark',
            ellipsis: true
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id'
        },
        treeList: [],
        treeLoading: true,
        gatewayList: []
      }
    },
    created () {
      this.getTreeList()
    },
    methods: {
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      getTreeList () {
        tree(Object.assign(this.queryParam)).then(res => {
          this.treeLoading = false
          if (!res.success) {
            return
          }
          this.treeList = res.data
        })
      },
      handleClick (e) {
        this.queryParam.gatewayConfigCode = e.toString()
        this.$refs.table.refresh()
      },
      statusFliter (text) {
        if (text === 1) {
          return '启用'
        } else if (text === 0) {
          return '禁用'
        }
      }
    }
  }
</script>

<style lang="less" scoped>
@import '../../../assets/css/list.less';

.table-page-search-wrapper{
  /deep/ .ant-row{
    margin-left: 0 !important;
  }
}

 .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }

  .ant-cascader-picker {
    background: #104E5C;
    border: 1px solid #104E5C;
    color: #fff;
    .ant-cascader-menus-content .ant-cascader-menu{
      background: #104E5C;
    }
  }
  .ant-cascader-menus{
    background: #104E5C;
  }

  .dipPic {
    width: 304px;
    height: 279px;
    margin: 80px auto 0 20px;
    background-image: url(~@/assets/img/dippicbg.png);
    background-repeat: no-repeat;
    background-size: 100% 100%;
  }

  .dipClass {
    width: 304px;
    margin: 50px auto 0;
    color: #fff;
    padding-left: 56px;

    li {
      margin-bottom: 10px;
      list-style: none;

      .dipTip {
        display: inline-block;
        width: 120px;
        height: 22px;
        line-height: 22px;
        padding-left: 10px;
        background-image: url(~@/assets/img/dipbg.png);
        background-repeat: no-repeat;
        background-size: 100% 100%;
        font-size: 21px;
      }

      .dipNum {
        display: inline-block;
        margin-left: 10px;
        color: #19ECFF;
        font-size: 25px;
        width: 80px;
        text-align: right;
      }
    }
  }
::v-deep.ant-pagination-item a{
  color: #fff !important;
}
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
